<template>
	<view class="pages">
		<view class="nav">
			<cu-custom :isBack="true" :isTar="true" bgColor="bg-gragients text-white">
				<block slot="leftText"></block>
				<block slot="content">操作管理</block>
				<block slot="righttar"></block>
			</cu-custom>
		</view>
		<view class="content">
			<view>
				<!-- 普通弹窗 -->
				<uni-popup ref="popup">
					<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }">

					</view>
				</uni-popup>
			</view>
			<view class="controls" v-if="control">
				<view class="map">
				</view>
				<view class="item" v-if="control_index">
					<view class="item_cont">
						<view class="item_head">
							<view class="head_tab" @click="changeOperateTab('all')">
								<text>全部</text>
								<view class="" v-if="isall">
								</view>
							</view>
							<view class="head_tab" @click="changeOperateTab('unstart')">
								<text>未开始</text>
								<view class="" v-if="isnostart">
								</view>
							</view>
							<view class="head_tab" @click="changeOperateTab('running')">
								<text>进行中</text>
								<view class="" v-if="isproce">
							
								</view>
							</view>
							<!-- <view class="head_tab" @click="changeOperateTab('end')">
								<text>已结束</text>
								<view class="" v-if="isend">
							
								</view>
							</view> -->
						</view>
						<view class="">
							<view class="" style="height: 640rpx;overflow: auto;">
								<view v-for="(workArea, index) in workAreaList" :key="index" class="item_li">
									<view class="li_left" @click="selecWorkArea(workArea)">
										<view class="text0" v-if="!!workArea.projectName">项目名称：{{workArea.projectName}}</view>
										<view class="text1" v-if="!!workArea.areaName">施工区域：{{workArea.areaName}}</view>
										<view class="text2" v-if="!!workArea.startTime">开始时间：{{workArea.startTime}}</view>
									</view>
									<view class="li_right">
										<view class="text3" v-if="workArea.workState == 0 || workArea.workState == 1000">未开始</view>
										<view class="text4" @click="start(workArea)" v-if="workArea.workState == 0 || workArea.workState == 1000">开始</view>
										<view class="text3" v-if="workArea.workState != 0 && workArea.workState != 1000">进行中</view>
										<view class="text5" @click="stop(workArea)" v-if="workArea.workState != 0 && workArea.workState != 1000">结束</view>
									</view>		
								</view>
							</view>
						</view>
						<view class="item_search" @click="to_search()" style="margin-top: 20rpx;">
							<input type="text" v-model="curProject.name" placeholder="请选择项目" />
							<image src="../../static/2/00.png" mode=""></image>
						</view>
						<view class="tab_items">
							<view class="tab_item">
								<view class="tab_item_img" @click="changeTab('4')">
									<image v-if="dataType == '4'" src="../../static/imgs/0813_03.png" mode=""></image>
									<image v-if="dataType != '4'" src="../../static/imgs/compaction.png" mode=""></image>
								</view>
								<view class="tab_item_text" >
									<text>压实</text>
								</view>
							</view>
							<view class="tab_item">
								<view class="tab_item_img" @click="changeTab('10')">
									<image v-if="dataType == '10'" src="../../static/imgs/0813_05.png" mode=""></image>
									<image v-if="dataType != '10'" src="../../static/imgs/paving.png" mode=""></image>
								</view>
								<view class="tab_item_text">
									<text>摊铺</text>
								</view>
							</view>
							<view class="tab_item">
								<view class="tab_item_img" @click="changeTab('5')">
									<image v-if="dataType == '5'" src="../../static/imgs/0813_07.png" mode=""></image>
									<image v-if="dataType != '5'" src="../../static/imgs/dynamicCompaction.png" mode=""></image>
								</view>
								<view class="tab_item_text">
									<text>强夯</text>
								</view>
							</view>
						</view>
					</view>
				</view>

				<view>
					<!-- 普通弹窗 -->
					<uni-popup ref="popup" @change="change">
						<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }">
							<view class="pop_box">
								<view class="pop_search">
									<text class="cuIcon-search mr10"></text>
									<input class="uni-search" type="text" v-model="filterProjectName" @confirm="search" placeholder="请输入项目" />
									<button class="text3" type="default" size="mini" @click="searchProject()">搜索</button>
									<!-- <input type="text" value="" placeholder="请输入项目" />
									<text class="text3">搜索</text> -->
								</view>
								<view class="pop_cont">
									<view class="pop_item" v-for="(project, index) in projectList" :key="index" @click="selectProject(project)">
										<text>{{project.name}}</text>
										<image src="../../static/2/20.png" mode=""></image>
									</view>
								</view>
							</view>
						</view>
					</uni-popup>
				</view>
				
				<view>
					<!-- 普通弹窗 -->
					<uni-popup ref="popupSelect" @change="change">
						<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }">
							<view class="xq">
								<view class="item">
									<view class="item_cont">
										<view class="title1">
											<text style="color: #C0C0C0;">项目名称</text>
											<text style="text-align: right;">{{curWorkArea.projectName}}</text>
										</view>
										<view class="title1" style="border: none;">
											<text style="color: #C0C0C0;">工区位置</text>
											<text style="text-align: right;">{{curWorkArea.areaName}}</text>
										</view>
									</view>
									
									<view class="item_cont2">
										<view class="cont" style="border-bottom: 1rpx #C0C0C0 solid;">
											<view class="cont_li" style="width: 80rpx;">
												<text class="title0">序号</text>
											</view>
											<view class="cont_li" style="width: 280rpx;">
												<text class="title0">开始时间</text>
											</view>
											<view class="cont_li" style="width: 280rpx;">
												<text class="title0">结束时间</text>
											</view>
											<view class="cont_li" style="width: 100rpx;">
												<text class="title0">工作时长</text>
											</view>
														
										</view>
										
										<view class="" style="width: 740rpx;height: 800rpx;overflow: auto;">
											<view class="cont" v-for="(workAreaProcess, index) in workAreaProcessList" :key="index">
												<view class="cont_li" style="width: 40rpx;">
													<text>{{index + 1}}</text>
												</view>
												<view class="cont_li" style="width: 260rpx;">
													<text>{{workAreaProcess.startTime}}</text>
												</view>
												<view class="cont_li" style="width: 260rpx;">
													<text>{{workAreaProcess.endTime}}</text>
												</view>
												<view class="cont_li" style="width: 140rpx;">
													<text>{{workAreaProcess.duration}}</text>
												</view>
															
											</view>
											
										</view>
										
									</view>
								</view>
							</view>
						</view>
					</uni-popup>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				banner: [],
				control: true,
				mark: false,
				history: false,
				type: 'bottom',
				
				// 实时监控
				isall: true,
				isnostart: false,
				isproce: false,
				isend: false,
				
				isimg: false,
				isimg2: false,
				datetimerange: [],
				control_index: true,
				
				
				quanbu:true,
				manhang:false,
				dianhang:false,
				filterProjectName: '',
				
				curProject: {},
				projectList: [],
				allProjectList: [],
				
				curWorkArea: {},
				workAreaList: [],
				
				workAreaProcessList: [],
				dataType: '4',
				
			}
		},
		onLoad() {
			this.getWorkAreaList();
			this.$HTTP('/projects/gets', '', 'GET').then((res) => {
				this.projectList = JSON.parse(JSON.stringify(res.data.obj));
				this.allProjectList = JSON.parse(JSON.stringify(res.data.obj));
			});
			
		},
		methods: {
			getFormatDate(smpTime, format) {
				let curDate = new Date(smpTime);
				var o = { 
					"M+": curDate.getMonth() + 1, 
					"d+": curDate.getDate(), 
					"h+": curDate.getHours(), 
					"m+": curDate.getMinutes(), 
					"s+": curDate.getSeconds(), 
					"q+": Math.floor((curDate.getMonth() + 3) / 3), 
					"S": curDate.getMilliseconds() 
				} 
				if (/(y+)/.test(format)) { 
					format = format.replace(RegExp.$1, (curDate.getFullYear() + "").substr(4 - RegExp.$1.length)); 
				} 
				for (let k in o) { 
					if (new RegExp("(" + k + ")").test(format)) { 
						format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); 
					} 
				} 
				return format; 
			},
			to_control() {
				this.control_index = true
				this.control = true
				this.mcontrol_index = false
				this.mark = false
				this.history = false
			},
			
			selecWorkArea(workArea) {
				this.curWorkArea = workArea;
				this.toselect();
			},
			
			todata() {
				this.$HTTP('/api/index', '', 'GET').then((res) => {
					uni.stopPullDownRefresh();
					this.banner = res.data.data.banner
					if (this.banner.length == 6) {
						this.ishide = false
						console.log(this.banner.length)
					}


				})
			},
			
			changeTab(dataType) {
				this.dataType = dataType;
				this.getWorkAreaList();
			},
			
			changeOperateTab(curTab) {
				this.isall = false;
				this.isnostart = false;
				this.isproce = false;
				this.isend = false;
				if (curTab === 'unstart') {
					this.isnostart = true;
					this.workState = 0;
				} else if (curTab === 'running') {
					this.isproce = true;
					this.workState = 400;
				} else if (curTab === 'end') {
					this.isend = true;
					this.workState = 1000;
				} else {
					this.isall = true;
					this.workState = null;
				}
				this.getWorkAreaList();
			},
			
			getWorkAreaProcessList() {
				const params = {
					"measureAreaId": this.curWorkArea.id,
					"dataType": this.curWorkArea.dataType,
				};
				this.$HTTP('/project/area/selectWorkAreaProcessList', params, 'POST').then((res) => {
					if (res && res.data && res.data.result) {
						let workAreaProcessList = res.data.obj || [];
						workAreaProcessList.forEach(workAreaProcess=> {
							workAreaProcess['duration'] = this.calculationDuration(workAreaProcess);
						})
						this.workAreaProcessList = workAreaProcessList;
					} else {
						this.workAreaProcessList = [];
					}
				
				});
			},
			
			calculationDuration(workAreaProcess) {
				let duration = 0;
				if (!!workAreaProcess.endTime) {
					duration =  (new Date(workAreaProcess.endTime).getTime() - new Date(workAreaProcess.startTime).getTime());
				} else {
					duration =  (new Date().getTime() - new Date(workAreaProcess.startTime).getTime());
				}
				return (duration/1000/60/60).toFixed(1) + '小时';
			},
			
			getUUID(){
			    let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
			    let uuid = new Array(36),
			        rnd = 0,
			        r;
			    for (var i = 0; i < 36; i++) {
			        if (i === 8 || i === 13 || i == 18 || i == 23) {
			            uuid[i] = '-';
			        } else if (i === 14) {
			            uuid[i] = '4';
			        } else {
			            if (rnd <= 0x02){
							rnd = 0x2000000 + (Math.random() * 0x1000000) | 0;
							}
			            r = rnd & 0xf;
			            rnd = rnd >> 4;
			            uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
			        }
			    }
			    return uuid.join('').replace(/-/gm, '').toLowerCase();
			},
			
			start(workArea) {
				let curDate = this.getFormatDate(new Date().getTime(), 'yyyy-MM-dd hh:mm:ss');
				let params = {
					"agreeStartTime": curDate,
					"id": this.getUUID(),
					"groupName": workArea.areaName,
					"departName": workArea.projectName || this.curProject.name || "",
					"state":  this.dataType == 5 ? 200 : 400,
					"measureAreaId": workArea.id,
				};
				this.$HTTP('/compaction/addCompactionProcess', params, 'POST').then((res) => {
					if (res && res.data && res.data.result) {
						workArea.startTime = curDate;
						this.getWorkAreaList();
						// this.workAreaList = res.data.obj.items || [];
					}
				
				});
			},			
			stop(workArea) {
				let curDate = this.getFormatDate(new Date().getTime(), 'yyyy-MM-dd hh:mm:ss');
				let params = {
					"applyEndTime": curDate,
					"id": workArea.processId,
					"deviceName": "" ,
					"groupName": workArea.areaName,
					"departName": workArea.projectName || this.curProject.name || "",
					"state": this.dataType == 5 ? 400 : 1000,
					"measureAreaId": workArea.id,
				};
				this.$HTTP('/compaction/addCompactionProcess', params, 'POST').then((res) => {
					if (res && res.data && res.data.result) {
						workArea.endTime = curDate;
						this.getWorkAreaList();
						// this.workAreaList = res.data.obj.items || [];
					}
				
				});
			},
			
			getWorkAreaList() {				
				let params = {
					"pageCurrent": 1,
					"pageSize": 50,
					"sectionCode": this.curProject.projectCode || "",
					"dataType": this.dataType,
					"workState": this.workState,
				};
				this.$HTTP('/project/area/selectWorkAreaList', params, 'POST').then((res) => {
					if (res && res.data && res.data.success) {
						this.workAreaList = res.data.obj.items || [];
					}
				
				});
			},
			
			selectProject(project) {
				this.curProject = project;
				this.$refs.popup.close(this.type);
				this.getWorkAreaList();
			},
			
			searchProject() {
				this.projectList = this.allProjectList.filter(project=>{
					return (project.name.indexOf(this.filterProjectName.trim()) > -1);
				})
			},
			
			// index
			to_all() {
				this.isall = true
				this.isnostart = false
				this.isproce = false
				this.isend = false
			},
			to_nostart() {
				this.isall = false
				this.isnostart = true
				this.isproce = false
				this.isend = false
			},
			to_proce() {
				this.isall = false
				this.isnostart = false
				this.isproce = true
				this.isend = false
			},
			to_end() {
				this.isall = false
				this.isnostart = false
				this.isproce = false
				this.isend = true
			},
			toselect() {
				this.getWorkAreaProcessList();
				this.type = 'bottom'
				this.$refs.popupSelect.open(this.type)
				// this.$refs.popup.close(this.type)
			},
			


			to_search() {
				this.type = 'bottom'
				// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
				this.$refs.popup.open(this.type)

				// this.$refs.popupCar.open(this.type)
			},
			
			
			
			
			change(e) {
				console.log('当前模式：' + e.type + ',状态：' + e.show);
			},

		}
	}
</script>

<style lang="scss">
	.pages {
		background-image: url('http://picture.ik123.com/uploads/allimg/170629/12-1F629112239.jpg'); //网络图片
		height: 100vh;
		background-size: 100% 100%;
		position: relative;
	}

	.tabbox {
		width: 100%;
		height: 80rpx;
		background-color: rgba(0, 0, 0, 0.8);
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;

		.tab {
			width: 250rpx;
			height: 50rpx;
			line-height: 50rpx;
			// background-color: #4CD964;
			border-right: 1rpx #808080 solid;
			display: flex;
			flex-direction: row;
			justify-content: center;
			align-items: center;

			image {
				width: 40rpx;
				height: 40rpx;

			}

			text {
				color: #FFFFFF;
				text-align: center;
				font-size: 24rpx;
				margin-left: 30rpx;
			}
		}
	}

	.content {}

	.controls {
		width: 750rpx;

		.map {}

		.item {
			width: 750rpx;
			// height: 730rpx;

			position: absolute;
			bottom: 0;
			left: 0;

			.item_cont {
				margin: 10rpx;
				padding-top: 20rpx;
				height: 1000rpx;
				background-color: #FFFFFF;
				border-radius: 20rpx;
				.item_search {
					margin: 0rpx 40rpx;
					height: 60rpx;
					background-color: #e6e6e6;
					border-radius: 60rpx;
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					overflow: hidden;

					input {
						width: 500rpx;
						margin-left: 80rpx;
						text-align: center;
						font-size: 24rpx;
					}

					image {
						width: 60rpx;
						height: 60rpx;
					}
				}
				
				
				.item_head {
					margin: 10rpx auto 0;
					width: 600rpx;
					height: 60rpx;
					font-size: 20rpx;
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;

					.head_tab {
						display: flex;
						justify-content: space-between;
						align-items: center;
						flex-direction: column;
						height: 40rpx;

						&:active {
							color: #0251FF;
						}
						
						text {
							font-size: 28rpx;
						}

						view {
							width: 40rpx;
							border-radius: 10rpx;
							height: 4rpx;
							background-color: #0251ff;
						}
					}
				}

				.item_li {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					padding: 0 10rpx;
					height: 180rpx;
					font-size: 20rpx;
					margin: 0 auto;
					border-bottom: 1rpx #e6e6e6 solid;
					width: 600rpx;
					.li_left{
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						align-items: left;
						height: 120rpx;
						width: 450rpx;
						border-right: 1rpx #e6e6e6 solid;
					}
					.li_right{
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						align-items: center;
						height: 120rpx;
						width: 150rpx;
						// background-color: #4CD964;
					}
					.text0 {
						width: 400rpx;
						text-align: left;
						font-size: 28rpx;
						margin-top: 10rpx;
					}
					.text1 {
						width: 400rpx;
						text-align: left;
						font-size: 24rpx;
						margin-top: 10rpx;
						// background-color: #007AFF;
					}

					.text2 {
						width: 400rpx;
						text-align: left;
						color: #C0C0C0;
						font-size: 20rpx;
						margin-bottom: 10rpx;
						// background-color: #007AFF;
					}
					
					.text3 {
						width: 200rpx;
						text-align: center;
						color: #C0C0C0;
						margin-top: 10rpx;
						// background-color: #007AFF;
					}
					
					.text4 {
						width: 150rpx;
						text-align: center;
						background-color: #007AFF;
						margin-bottom: 10rpx;
						width: 100rpx;
						border-radius: 30rpx;
						color: #FFFFFF;
					}
					
					.text5 {
						width: 150rpx;
						text-align: center;
						background-color: #000000;
						margin-bottom: 10rpx;
						width: 100rpx;
						border-radius: 30rpx;
						color: #ffffff;
					}

					
				}
			}


		}

		.pop_box {
			width: 100%;
			height: 82vh;
			padding-top: 40rpx;
			background-color: #FFFFFF;
			border-top-left-radius: 20rpx;
			border-top-right-radius: 20rpx;

			.pop_search {
				margin: 0 30rpx;
				height: 60rpx;
				padding-left: 30rpx;
				background-color: #e6e6e6;
				border-radius: 60rpx;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;

				.text3 {
					width: 120rpx;
					height: 52rpx;
					border-radius: 60rpx;
					margin-right: 4rpx;
					background-color: #FFFFFF;
					font-size: 24rpx;
					line-height: 52rpx;
					text-align: center;
				}

				input {
					width: 500rpx;
					text-align: left;
					font-size: 24rpx;
				}
			}

			.pop_cont {
				margin: 30rpx;
				padding-top: 30rpx;
				height: 890rpx;
				// background-color: #007AFF;
				overflow: auto;
				box-shadow: 0px 0px 20rpx 5rpx #e6e6e6;

				.pop_item {
					margin: 0rpx 10rpx 0;
					padding: 10rpx 20rpx;
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					border-bottom: 1rpx #e6e6e6 solid;

					text {
						font-size: 24rpx;
					}

					image {
						width: 40rpx;
						height: 40rpx;
					}
				}
			}
		}

		
		
		.xq {
			.item{
				width: 750rpx;
				height: 1100rpx;
				background-color: #FFFFFF;
				border-top-left-radius: 20rpx;
				border-top-right-radius: 20rpx;
				.item_cont {
					width: 730rpx;
					height: 140rpx;
					margin: 0 10rpx;
					// position: absolute;
					// bottom: 0;
					// left: 0;
					
					
					border-radius: 20rpx;
				
					overflow: hidden;
						
					.title1 {
						// width: 730rpx;
						height: 60rpx;
						display: flex;
						flex-direction: row;
						justify-content: space-between;
						align-items: center;
						font-size: 26rpx;
						padding: 0 40rpx;
						border-bottom: 1rpx #C0C0C0 solid;
						// background-color: #007AFF;
					}
						
					.cont {
						width: 730rpx;
						display: flex;
						flex-direction: row;
						justify-content: flex-start;
						align-items: center;
						margin: 10rpx;
						.cont_li {
							// width: 200rpx;
							display: flex;
							flex-direction: column;
							justify-content: center;
							align-items: center;
							font-size: 20rpx;
							height: 70rpx;
						
							// line-height: 100rpx;
							.title0 {
								color: #C0C0C0;
							}
							text{
								text-align: center;
							}
						}
					}
				}
				.item_cont2 {
					width: 730rpx;
					height: 800rpx;
					margin: 10rpx;
					box-shadow: 0px 0px 20rpx 5rpx #e6e6e6;
					background-color: #FFFFFF;
					display: flex;
					flex-direction: column;
					justify-content: flex-start;
					align-items: center;
					border-radius: 20rpx;
					overflow: hidden;
					.cont {
						width: 730rpx;
						display: flex;
						flex-direction: row;
						justify-content: flex-start;
						align-items: center;
						margin: 10rpx;
						.cont_li {
							// width: 200rpx;
							display: flex;
							flex-direction: column;
							justify-content: center;
							align-items: center;
							font-size: 20rpx;
							height: 40rpx;
						
							// line-height: 100rpx;
							.title0 {
								color: #C0C0C0;
							}
							text{
								text-align: center;
							}
						}
					}
				}
			}
		}
		
		.tab_items {
			margin-top: 20rpx;
			display: flex;
			justify-content: space-around;
			.tab_item {
				display: flex;
				flex-direction: column;
				.tab_item_img {
					image {
						width: 80rpx;
						height: 80rpx;
					}
				}
				.tab_item_text {
					text {
						padding-left: 5px;
						font-size: 14px;
					}
				}
			}
		}
	}
	
	.mr10 {
		margin-right: 20rpx;
	}


</style>
